using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
//游戏管理器 （全局单例）
public class GameManager : MonoSingleton<GameManager>
{
    public GameObject Player { get; set; }

    public bool isSound { get; set; }
    public bool isMusic { get; set; }

    public int Score { get; private set; }
    public GameObject PlayerDisplayCanas;
    public List<ItemDatas_SO> bag = new List<ItemDatas_SO>();
    public GameObject bagUI;//背包界面 方便直接操作背包
    public string[] levels = { "Level1", "Level2", "Level3" };
    public Vector3 PlayerPosition()
    {
        return Player.transform.position;
    }

    public event System.Action<int> OnScoreChanged; // 任意场景都可订阅

    public void AddScore(int v)
    {
        Score += v;
        OnScoreChanged?.Invoke(Score);
    }
    //打开失败ui
    public void OpenLoseUI(bool gameover = false)
    {

        if (PlayerDisplayCanas != null)
        {
            PlayerDisplayCanas.GetComponent<GameUIControl>().viewGameOverMenu(gameover);
        }
        else
        {
            GameObject.Find("Player Display Canvas").GetComponent<GameUIControl>().viewGameOverMenu(gameover);
        }
    }

    //下一关
    public void PlayNextGame()
    {
        SceneManager.LoadScene("gameScene2");
    }

    //往背包添加物品 如果有相同的物品 num+1 否则添加物品到背包
    public void AddItem(ItemDatas_SO item)
    {
        foreach (ItemDatas_SO i in bag)
        {
            if (i.itemId == item.itemId)
            {
                i.itemNum += 1;
                return;
            }
        }
        item.itemNum = 1;
        bag.Add(item);
    }

    //删除背包物品
    public void ReItem(ItemDatas_SO item)
    {
        foreach (ItemDatas_SO i in bag)
        {
            if (i.itemId == item.itemId && i.itemNum > 1)
            {
                i.itemNum -= 1;
                return;
            }
        }
        bag.Remove(item);
    }

    //为了刷新背包物品
    public void RefreshBag()
    {
        bagUI.GetComponent<BagUIPage>().setBagItems();
    }
}
